package com.ztgf.file.fastdfs;

import com.github.tobato.fastdfs.domain.MateData;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ztgf.common.utils.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Set;

@Component
public class FastdfsUtils {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;


    private static  String  prefixed="http://xxxx.xxxx.com";
    /**
     *	MultipartFile类型的文件上传ַ 测试
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFilePath(MultipartFile file) throws IOException {
        String fileName = "11111111111";
        String extName = "jpeg";
//        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),
//                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        String s = storePath.getPath() + "/n";
        storePath = fastFileStorageClient.uploadSlaveFile(storePath.getGroup(), storePath.getPath(), file.getInputStream(), file.getSize(),fileName, extName);
        s += storePath.getPath() + "/n";
        return s;
    }

    /**
     *	MultipartFile类型的文件上传ַ
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        String fileName = "aaaa200200.jpeg";
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return getResAccessUrl(storePath);
    }
    /**
     * 普通的文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        StorePath path = fastFileStorageClient.uploadFile(inputStream, file.length(),
                FilenameUtils.getExtension(file.getName()), null);
        return getResAccessUrl(path);
    }

    /**
     * 普通的文件上传
     *
     * @param bytes 文件流
     * @param size 文件大小
     * @param fileExtName 文件扩展名
     * @return
     * @throws IOException
     */
    public String uploadFileByte(byte[] bytes, Long size, String fileExtName) throws IOException {
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        StorePath path = fastFileStorageClient.uploadFile(stream, size, fileExtName, null);
        return getResAccessUrl(path);
    }
    /**
     * 带输入流形式的文件上传
     *
     * @param is
     * @param size
     * @param fileExtName
     * @return
     */
    public String uploadFileStream(InputStream is, long size, String fileExtName) {
        StorePath path = fastFileStorageClient.uploadFile(is, size, fileExtName, null);
        return getResAccessUrl(path);
    }

    /**
     * 带输入流形式的文件上传
     *
     * @param is
     * @param size
     * @param fileExtName
     * @return
     */
    public String uploadSlaveFileStream(String groupName, String pathName, InputStream is, long size, String fileName, String fileExtName) {
        StorePath path = fastFileStorageClient.uploadSlaveFile(groupName, pathName, is, size, fileName, fileExtName);
        return getResAccessUrl(path);
    }


    /**
     * 带输入流形式的文件上传
     *
     * @param bytes
     * @param size
     * @param fileExtName
     * @return
     */
    public String uploadSlaveFileStream(String groupName, String pathName, byte[] bytes, long size, String fileName, String fileExtName) {
        InputStream is = FileUtils.byte2InputStream(bytes);
        return uploadSlaveFileStream(groupName, pathName, is, size, fileName, fileExtName);
    }
    /**
     * 将一段文本文件写到fastdfs的服务器上
     *
     * @param content
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath path = fastFileStorageClient.uploadFile(stream, buff.length, fileExtension, null);
        return getResAccessUrl(path);
    }

    /**
     * 返回文件上传成功后的地址名称ַ
     * @param storePath
     * @return
     */
    private String getResAccessUrl(StorePath storePath) {
        return "/"+storePath.getGroup()+"/"+storePath.getPath();
    }

    /**
     * 删除文件
     * @param fileUrl
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.praseFromUrl(fileUrl);
            fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            System.out.println("error"+e.getMessage());
        }
    }
    public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
        StorePath path = fastFileStorageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
        return getResAccessUrl(path);
    }

    public InputStream downFile(String groupName, String path){
        DownloadByteArray byteArray = new DownloadByteArray();
        byte[] bytes = fastFileStorageClient.downloadFile(groupName, path, byteArray);
        InputStream inputStream = new ByteArrayInputStream(bytes);
        return inputStream;
    }

    public byte[] downFileByte(String groupName, String path){
        DownloadByteArray byteArray = new DownloadByteArray();
        return fastFileStorageClient.downloadFile(groupName, path, byteArray);
    }
}
